package com.xindaoapp.happypet.ui;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.FreeUseListData;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.RoundImageView;

/**
 * 免费试用
 * 
 * @author Stone
 * 
 */
public class FreeUseActivity extends BaseActivity {

    private PullToRefreshListView mPullToRefreshListView;
    private FreeUseAdapter mAdapter;
    private RelativeLayout mNoData;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        // TODO Auto-generated method stub
        return R.layout.activity_free_use;
    }

    @Override
    protected void initViews() {

        mContext = this;

        mNoData = (RelativeLayout) this.findViewById(R.id.no_data);

        mPullToRefreshListView = (PullToRefreshListView) this.findViewById(R.id.listview);
        mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
        mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                onLoadDatas();
            }
        });


        super.initViews();
    }


    @Override
    protected void initEvents() {
        // TODO Auto-generated method stub
        super.initEvents();
    }

    @Override
    protected void onLoadDatas() {
        // TODO Auto-generated method stub
        super.onLoadDatas();
        getData();
    }

    private void getData() {

        getMoccaApi().getFreeUseListDataResult(Constants.PAGE_FIRST + "", Constants.PAGE_SIZE + "",
                new IRequest<List<FreeUseListData>>() {

                    @Override
                    public void request(List<FreeUseListData> t) {

                        if (t != null) {
                            if (t.size() == 0) {
                                mNoData.setVisibility(View.VISIBLE);
                            } else {
                                onDataArrived(true);
                                mNoData.setVisibility(View.GONE);
                                mAdapter =
                                        new FreeUseAdapter(FreeUseActivity.this, t,
                                                Constants.PAGE_SIZE, R.layout.freeuse_list_item,
                                                R.layout.item_loading);
                                mPullToRefreshListView.setAdapter(mAdapter);
                            }

                        } else {
                            onDataArrived(false);
                        }
                        mPullToRefreshListView.onRefreshComplete();
                    }
                });


    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                finish();
            }
        };
    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected String getTopBarTitleStrRes() {
        return "免费试用";
    }


    // 适配器
    private class FreeUseAdapter extends XinDaoBaseAdapter<FreeUseListData> {

        public FreeUseAdapter(Context context, List<FreeUseListData> iniData, int pageSize,
                int res, int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent,
                final FreeUseListData value) {
            ViewHolder holder = (ViewHolder) convertView.getTag();;
            if (holder == null) {
                holder = new ViewHolder();
                holder.pic = (RoundImageView) convertView.findViewById(R.id.pic);
                holder.num1 = (TextView) convertView.findViewById(R.id.num1);
                holder.num2 = (TextView) convertView.findViewById(R.id.num2);
                holder.endTime = (TextView) convertView.findViewById(R.id.endtime);
                convertView.setTag(holder);
            }

            int hgt = CommonParameter.sScreenWidth * 320 / 612;

            if (!value.getCoverwidth().equals("0") && !value.getCoverwidth().equals("")
                    && !value.getCoverheight().equals("") && !value.getCoverheight().equals("0")) {
                hgt =
                        CommonParameter.sScreenWidth * Integer.parseInt(value.getCoverheight())
                                / Integer.parseInt(value.getCoverwidth());
            }



            holder.pic.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, hgt));



            ImageLoader.getInstance().displayImage(value.getCoverpath(), holder.pic);



            holder.num1.setText(value.getNumber());
            holder.num2.setText(value.getApplynumber());
            // 结束时间
            holder.endTime.setText(CommonUtil.formatTimeForFreeUse(1000 * Long.parseLong(value
                    .getStarttimeto().trim())));

            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PostDetailActivity.class);
                    intent.putExtra(Constants.KEY_POST_TID, value.getTid());
                    intent.putExtra(Constants.KEY_FROM_PAGE, 1);// 这里可以作区分
                    mContext.startActivity(intent);
                }
            });
            return convertView;
        }

        @Override
        public void nextPage(
                int start,
                int size,
                final com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<FreeUseListData> iLoadNextPageData) {
            HappyPetApplication
                    .get()
                    .getMoccaApi()
                    .getFreeUseListDataResult(String.valueOf(start), String.valueOf(size),
                            new IRequest<List<FreeUseListData>>() {
                                @Override
                                public void request(List<FreeUseListData> t) {
                                    iLoadNextPageData.loadNextPageData(t);
                                }
                            });
        }
    }
    private class ViewHolder {
        RoundImageView pic;
        TextView num1, num2, endTime;
    }
    //



}
